package com.globant.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.globant.adapter.JobAdapter.JobViewHolder;
import com.globant.bean.PlayerBean;
import com.globant.tab.BattleTabActivity;
import com.globant.tab.R;

public class BattleAdapter extends BaseAdapter {

	private List<PlayerBean> players;
	private BattleTabActivity battleTabActivity;
	
	public BattleAdapter(BattleTabActivity battleTabActivity, List<PlayerBean> players) {
		this.players = players;
		this.battleTabActivity = battleTabActivity;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return players.size();
	}

	@Override
	public Object getItem(int position) {
		return players.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		BattleViewHolder holder = null;
		
		if(convertView == null) {
			item = new View(parent.getContext());
		} else {
			item = (View) convertView;
		}
		
        if (item == null || !(item.getTag() instanceof JobViewHolder)) {
            
            LayoutInflater mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            item = mInflater.inflate(R.layout.battle_info, parent, false);
            
            holder = new BattleViewHolder();
            populateView(holder, item, players.get(position));
            item.setTag(holder);
       }else{
           holder = (BattleViewHolder) item.getTag();
       }

		return item;
	}
	
	public void populateView(BattleViewHolder holder, View item, PlayerBean player) {
		holder.playerName = (TextView) item.findViewById(R.id.playerName);
		holder.defensePoints = (TextView) item.findViewById(R.id.defensePoints);
		holder.attackPoints = (TextView) item.findViewById(R.id.attackPoints);
		holder.fight = (Button) item.findViewById(R.id.button1);

		holder.playerName.setText(player.getName());
		holder.defensePoints.setText(String.valueOf(player.getAttackPoints()));
		holder.attackPoints.setText(String.valueOf(player.getDefensePoints()));
		
		holder.fight.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				battleTabActivity.onCreateDialog(0);
				battleTabActivity.showDialog(0);
			}
		});
	}
	
	class BattleViewHolder {
		public TextView playerName;
		public TextView defensePoints;
		public TextView attackPoints;
		public Button fight;

	}

}
